<?php
/**
 * CategoryController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Blog_CategoryController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Blog
 */
class Blog_CategoryController extends Madoqua_Controller_Action_Abstract
{
    /**
     * category model
     *
     * @var Blog_Category
     */
    protected $_categoryModel;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load('Blog_Category');
        $this->_categoryModel = new Blog_Category();
        //model
    }

    /**
     * list categories
     */
    public function indexAction()
    {
        $this->view->categories = $this->_categoryModel->getList();
    }

    /**
     * view a category
     */
    public function readAction()
    {
        $this->view->category = $this->getCategory();
    }
    
    /**
     * get the requested category
     *
     * @return Zend_Db_Table_Row
     */
    private function getCategory()
    {
        $slug = $this->getRequest()->getParam('slug', '');
        $category = $this->_categoryModel->fetchRow($this->_categoryModel->select()->where('slug = ?', $slug));
        if ($category === null) {
            throw new Exception('Category not found: ' . $slug);
        }
        return $category;
    }
}